package com.example.chenhaosblog.controller;

import com.example.chenhaosblog.base.BaseController;
import com.example.chenhaosblog.bean.User;
import com.example.chenhaosblog.service.IUserService;
import com.example.chenhaosblog.utils.PageData;
import com.example.chenhaosblog.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userController")
public class UserController extends BaseController {
    @Autowired
    private IUserService service;

 /*   @Autowired
    private RestTemplate restTemplate ;  //通过这个类调用其它模块的接口*/
    @RequestMapping("/userList")
    public List<User> getUserList(){


   /*     //调用其它boot的接口
        String url = "http://localhost:8087/test2/userList";
        User[] users =  restTemplate.getForObject(url,  User[] .class);
        List<User> userList = Arrays.asList(users);*/
        return service.findAllUser();
    }


    @RequestMapping("/add")
    public String add(PageData pageData){
        PageData result = new PageData();

        try {
            pageData.put("id", UuidUtil.get32UUID());
            service.add(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }


    @RequestMapping("/userList2")

   // @Cacheable(value="test11")
    public PageData getUserLis2t() throws Exception {

        PageData pageData = new PageData();
        List<PageData> all = service.findAll(pageData);
        pageData.put("data",all);
        return pageData;
    }




    @RequestMapping("/toLogin")
    @ResponseBody
    public String toLogin() throws Exception{
        PageData result = new PageData();

        PageData pageData = getPageData();
        try {
              pageData.put("id",getUserLis2t());
              service.add(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";

    }

}
